#!/usr/bin/perl

=head1 NAME

rssfeed.pl - generate email related rss feeds

=head1 DESCRIPTION

Creates two feeds, email.rss and spam.rss, which contain the most recent stats on
spam and email.  Useful for inclusion in RSS readers or on the website.

=head1 DEPENDENCIES

L<MLDBM>, L<XML::RSS>

=cut

require configvars;                  # Found via PERL5LIB env variable
use MLDBM qw( DB_File Storable );    # use Storable for serializing
use Fcntl qw(:DEFAULT);
use POSIX qw(strftime);
use XML::RSS;
use strict;

=head1 SYNOPSIS 
                    
  perl rssfeed.pl     
                        
=cut

our ( %database );
my ( $emailcounter, $spamcounter, $rss);

my $filter = '[\!|\@|\#|\$\%|\^|\*|\(|\)|\~|\"|\<|\>|\?|\=|\[|\]|\-|\:|\;|\`|\'|\,|\.|\^[|\^F|\^D\|^A|\^C|\£]';

tie %database, 'MLDBM', $MauchConfig::maildatabase, O_CREAT | O_RDWR, 0640 or die "Cannot open $MauchConfig::maildatabase $!\n";
open( EMAIL, ">" . $MauchConfig::emailrssfile ) or die "cannot open file for reading: $!";

$rss = new XML::RSS( version => '2.0' );
$rss->channel( title          => 'Charles Mauch\'s Email',
               link           => 'http://charles.mauch.name',
               language       => 'en',
               description    => 'Charles hates spammers',
               rating         => '(PICS-1.1 "http://www.classify.org/safesurf/" 1 r (SS~~000 1))',
               copyright      => 'Copyright 2006, Charles Mauch',
               pubDate        => GetRssDate( time ),
               lastBuildDate  => GetRssDate( time ),
               managingEditor => 'cmauch@gmail.com',
               webMaster      => 'cmauch@gmail.com'
             );
for my $record ( sort { $b cmp $a } keys %database ) {
    last if ( $emailcounter eq 50 );
    my $ref = $database{ $record };
    if ( $ref->{ 'type' } eq 'personal' ) {
        emailitem( $ref->{ 'date' }, $ref->{ 'score' }, $ref->{ 'from' }, $ref->{ 'email' }, $ref->{ 'subject' } );
        $emailcounter++;
    }
} ## end for my $record ( sort {...
print EMAIL $rss->as_string;
close EMAIL;

open( SPAM, ">" . $MauchConfig::spamrssfile ) or die "cannot open file for reading: $!";
$rss = new XML::RSS( version => '2.0' );
$rss->channel( title          => 'Charles Mauch\'s Spam',
               link           => 'http://charles.mauch.name',
               language       => 'en',
               description    => 'Charles hates spammers',
               rating         => '(PICS-1.1 "http://www.classify.org/safesurf/" 1 r (SS~~000 1))',
               copyright      => 'Copyright 2006, Charles Mauch',
               pubDate        => GetRssDate( time ),
               lastBuildDate  => GetRssDate( time ),
               managingEditor => 'cmauch@gmail.com',
               webMaster      => 'cmauch@gmail.com'
             );

for my $record ( sort { $b cmp $a } keys %database ) {
    last if ( $spamcounter eq 50 );
    my $ref = $database{ $record };
    if ( $ref->{ 'score' } > 4.9 ) {
       spamitem( $ref->{ 'date' }, $ref->{ 'score' }, $ref->{ 'from' }, $ref->{ 'email' }, $ref->{ 'subject' } );
        $spamcounter++;
    }
} ## end for my $record ( sort {...

print SPAM $rss->as_string;
close SPAM;

untie %database;

sub emailitem {
    my ( $date, $score, $from, $email, $subject ) = @_;
    $subject =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//gs;
    $subject =~ s/($filter)//gs;

    return
      $rss->add_item(
        title => "&lt;a href=\"mailto:$from &lt;$email&gt\" title=\"$from sucks!!\"  &gt;$from&lt;/a&gt; managed to score &lt;strong&gt;$score&lt;/strong&gt;",
        description => "&lt;p style=\"margin: 0;\"&gt; Subject: $subject &lt;/p&gt; ",
        pubDate     => GetRssDate( $date ),
      );
} ## end sub emailitem

sub spamitem {
    my ( $date, $score, $from, $email, $subject ) = @_;
    $subject =~ s/<(?:[^>'"]*|(['"]).*?\1)*>//gs;
    $subject =~ s/($filter)//gs;

    return
      $rss->add_item(
        title =>
          "&lt;a href=\"mailto:$from &lt;$email&gt\" title=\"$from sucks!!\"  &gt;$from&lt;/a&gt; managed to score &lt;strong&gt;$score&lt;/strong&gt;",
        description => "&lt;p style=\"margin: 0;\"&gt; Subject: $subject &lt;/p&gt; ",
        pubDate     => GetRssDate( $date ),
      );
} ## end sub rssitem 

sub GetRssDate {
    return strftime "%a, %d %b %Y %T GMT", gmtime( shift );
}


